package Arrays;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Exercise {
    public static void main(String[] args) {
        Book books[] = new Book[4];
        books[0] = new Book("红楼梦",100);
        books[1] = new Book("金瓶梅",90);
        books[2] = new Book("青年文摘",5);
        books[3] = new Book("JAVA从入门到放弃",300);

        /*//从小到大
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                double priceDif = book1.getPrice() - book2.getPrice();
                if(priceDif > 0){
                    return 1;
                }else if(priceDif < 0){
                    return  -1;
                }else {
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));*/


        //从大到小
        //从小到大
        /*Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                double priceDif = book1.getPrice() - book2.getPrice();
                if(priceDif > 0){
                    return -1;
                }else if(priceDif < 0){
                    return  1;
                }else {
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));*/


        //按照书名长度排序
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book book1 = (Book) o1;
                Book book2 = (Book) o2;
                return book2.getName().length() - book1.getName().length();
            }
        });
        System.out.println(Arrays.toString(books));


        //自己模拟
        /*bubble(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                int n1 = (Integer) o1;
                int n2 = (Integer) o2;
                return n1 - n2;
            }
        });
        System.out.println(Arrays.toString(books));*/
    }

    /*public static void bubble(Book books[], Comparator c){
        Book tmp = new Book("",0);
        for (int i = 0;i< books.length-1;i++){
            boolean flag = false;
            for (int j = 0;j< books.length - i - 1;j++){
                if(c.compare(books[j].getPrice(),books[j+1].getPrice()) > 0){
                    tmp = books[j];
                    books[j] = books[j+1];
                    books[j+1] = tmp;
                    flag = true;
                }
            }
            if(flag == false){
                break;
            }
        }
    }*/
}

class Book{
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "{" + name +"  " + price + "}";
    }
}
